﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

using System.Collections;

namespace SPBusiness
{
    public sealed class ContentType
    {
       /// <summary>
       /// ContentType class has only Static Methods, no constructor needed.
       /// </summary>
       private ContentType() { }
 
        public static void AddSiteColumn(string pSiteUrl, string pWebUrl, string pContentTypeName, string pStaticFieldName, int pOrder)
        {

            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
               //{

                   using (SPSite vSPSite = new SPSite(pSiteUrl))
                   {
                       using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                       {

                           SPField vSPField = null;

                           if (vSPWeb.IsRootWeb)
                           {
                               vSPField = vSPWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                           }
                           else
                           {
                               vSPField = vSPWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
                           }

                           if (vSPField != null)
                           {
                               if (vSPWeb.ContentTypes.OfType<SPContentType>().Where(x => x.Name.ToUpper() == pContentTypeName.ToUpper()).Any())
                               {
                                   SPContentType vSPContentType = vSPWeb.ContentTypes[pContentTypeName];

                                   if (!vSPContentType.Fields.OfType<SPField>().Where(x => x.StaticName == vSPField.StaticName).Any())
                                   {
                                       List<string> vList = new List<string>();
                                       for (int i = 0; i < vSPContentType.FieldLinks.Count; i++)
                                       {
                                           vList.Add(vSPContentType.FieldLinks[i].Name);
                                       }

                                       SPFieldLink vSPFieldLink = new SPFieldLink(vSPField);

                                       vSPContentType.FieldLinks.Add(vSPFieldLink);
                                       vSPContentType.Update(true);

                                       vList.Insert(pOrder, vSPField.InternalName);
                                       vSPContentType.FieldLinks.Reorder(vList.ToArray());
                                       vSPContentType.Update(true);

                                       vSPWeb.Update();
                                   }
                               }
                           }
                       }
                   }
               //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(ContentType).Namespace, typeof(ContentType).Name, "AddSiteColumn", ex);
                throw;
            }
        }

        public static void SetRequiredField(string pSiteUrl, string pWebUrl, string pContentTypeName, string pStaticFieldName, bool vRequired)
        {
            try
            {

                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{

    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
        {
            SPField vSPFieldSite = null;

            if (vSPWeb.IsRootWeb)
            {
                vSPFieldSite = vSPWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
            }
            else
            {
                vSPFieldSite = vSPWeb.Site.RootWeb.Fields.TryGetFieldByStaticName(pStaticFieldName);
            }

            if (vSPFieldSite != null)
            {
                if (vSPWeb.ContentTypes.OfType<SPContentType>().Where(x => x.Name.ToUpper() == pContentTypeName.ToUpper()).Any())
                {
                    SPContentType vSPContentType = vSPWeb.ContentTypes[pContentTypeName];

                    if (vSPContentType.Fields.OfType<SPField>().Where(x => x.StaticName == vSPFieldSite.StaticName).Any())
                    {
                        SPField vSPField = vSPContentType.Fields[vSPFieldSite.Id];

                        if (vSPField.Required != vRequired)
                        {
                            vSPField.Required = vRequired;
                            vSPField.Update();
                            vSPContentType.Update(true);
                            vSPWeb.Update();
                        }

                    }
                }

            }
        }
    }
//});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(ContentType).Namespace, typeof(ContentType).Name, "SetRequiredField", ex);
                throw;
            }
        }
    }
}
